package org.bambrikii.web.site.services.dictionary;

import java.util.Calendar;

import org.apache.commons.lang.xwork.StringUtils;
import org.bambrikii.kbManager.kb2.managers.ArtefactManager;
import org.bambrikii.kbManager.kb2.model.Artefact;
import org.bambrikii.kbManager.kb2.model.ArtefactAsText;
import org.bambrikii.site.base.business.managers.core.ManagerException;
import org.bambrikii.site.base.model.Pager;
import org.bambrikii.site.base.struts2.core.SiteBaseBean;

public class DictionaryBean extends SiteBaseBean {
	private static final int TXT_PAGER_COLS = 1;
	private static final int TXT_PAGER_ROWS = 150;

	public Pager<ArtefactAsText> getLanguages(final String title,
			final Integer currPage) throws ManagerException {
		final ArtefactManager mgr = getContainerBean().getManagerContainer()
				.getManager(ArtefactManager.class);
		return mgr
				.getLanguages(title, currPage, TXT_PAGER_COLS, TXT_PAGER_ROWS);
	}

	public Pager<ArtefactAsText> getPager(final String title,
			final Integer currPage) throws ManagerException {
		final ArtefactManager mgr = getContainerBean().getManagerContainer()
				.getManager(ArtefactManager.class);
		return mgr.getArtefacts(StringUtils.isEmpty(title) ? null : "%" + title
				+ "%", currPage, TXT_PAGER_COLS, TXT_PAGER_ROWS);
	}

	public ArtefactAsText addArtefact(final Integer sourceId,
			final Integer kindId, final String title, final String content)
					throws ManagerException {
		final ArtefactManager mgr = getContainerBean().getManagerContainer()
				.getManager(ArtefactManager.class);

		final Artefact source = mgr.getById(sourceId);
		final Artefact kind = mgr.getById(kindId);

		final Calendar cal = Calendar.getInstance();
		final ArtefactAsText txt = new ArtefactAsText();
		txt.setSource(source);
		txt.setKind(kind);
		txt.setKey(title);
		txt.setText(content);
		txt.setPublicationTime(cal.getTime());
		mgr.saveText(txt);
		return txt;
	}

	public Artefact getArtefact(final Integer id) throws ManagerException {
		final ArtefactManager mgr = getContainerBean().getManagerContainer()
				.getManager(ArtefactManager.class);
		return mgr.getById(id);
	}
}
